package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.UserRole;
import com.ccp.dev.system.service.UserRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户角色映射表 控制器类
 * @author y
 */
@Controller
@Action(ownermodel = SysAuditModelType.ROLE_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/userRole")
public class UserRoleController extends SysBaseController
{
	@Resource
	private UserRoleService userRoleService;

	/**
	 * 添加用户
	 * @param request HttpServletRequest
	 * @param roleId 角色id
	 * @return ResultData
	 */
	@Action(description = "角色增加用户",
			detail = "<#assign entity=sysRoleService.getOneById(roleId)/>角色【${entity.roleName}】增加用户<#if success>成功<#else>失败</#if>",
			exectype = "编辑日志")
	@ResponseBody
	@PostMapping(UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
	public ResultData addUser(HttpServletRequest request, @RequestParam("roleId") String roleId){
		try {
			String[] lAryId = RequestUtil.getStringAryByStr(request, "userId");
//			if(BeanUtils.isEmpty(lAryId)){
//				return getResultDataSuccess("operation.success");
//			}
			this.userRoleService.addUser(roleId,lAryId);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success");
		}catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("operation.failure");
		}
	}

	/**
	 * 删除用户角色映射表
	 * @param request HttpServletRequest
	 */
	@Action(description = "角色删除用户",
			detail = "角色删除用户<#if success>成功<#else>失败</#if>",
			exectype = "删除日志")
	@PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
	@ResponseBody
	public ResultData del(HttpServletRequest request) {
		try{
			String[] lAryId = RequestUtil.getStringAryByStr(request, "userRoleId");
			userRoleService.delByIdArray(lAryId);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success");
		}catch (Exception e) {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("operation.failure");
		}
	}


	/**
	 * 查询数据
	 * @param request HttpServletRequest
	 * @return ResultData
	 */
	@Action(description = "查询角色人员关联",
			detail = "查询角色人员关联<#if success>成功<#else>失败</#if>",
			exectype = "查询日志")
	@ResponseBody
	@GetMapping(UrlConstants.MODULE_FUNCTION_LIST)
	public ResultData queryList(HttpServletRequest request) {
		try {
			//needPage 根据前台使用方法不同特意传一个参数是否要分页  0 不分页 1 分页
			long needPage = RequestUtil.getLong(request,"needPage",1);
			QueryFilter queryFilter;
			if(needPage == 0){
				queryFilter = new QueryFilter(request,false);
			    List<UserRole> userRoleList = userRoleService.queryList(queryFilter);
				SysAuditThreadLocalHolder.putParamerter("success", true);
                return getResultDataSuccess("operation.success",0L,userRoleList);
			}
            queryFilter = new QueryFilter(request);
            List<UserRole> userRoleList = userRoleService.queryList(queryFilter);
			SysAuditThreadLocalHolder.putParamerter("success", true);
            return getResultDataSuccess("operation.success",queryFilter.getPageBean().getTotalCount(),userRoleList);
		} catch (Exception e) {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("operation.failure");
		}
	}
	/**
	 * 页面跳转 列表页
	 */
	@GetMapping(UrlConstants.VIEW_DISPATCH_PREFIX+"/list")
	public String viewList(){
		return "userRole/list";
	}

	/**
	 *  根据角色id获取角色下人员  姚陆洋
	 * @param request
	 * @return
	 */
	@Action(description = "查询角色人员关联",
			detail = "<#assign entity=sysRoleService.get(roleId)/>根据角色【${entity.roleName}】查询角色下人员信息<#if success>成功<#else>失败</#if>",
			exectype = "查询日志")
	@RequestMapping(UrlConstants.MODULE_FUNCTION_GET_USER)
	@ResponseBody
	public ResultData getUserListByRoleId(HttpServletRequest request){
		try {
			String roleId=RequestUtil.getString(request,"roleId");
			List<UserRole> list = userRoleService.queryUserByRoleIds(roleId);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success",0,list);
		}catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("operation.failure");
		}

	}
}
